//
//  TuyaViewController.m
//  HBuilder
//
//  Created by arthur on 2021/9/23.
//  Copyright © 2021 DCloud. All rights reserved.
//

#import "TuyaViewController.h"
#import <TYModuleServices/TYModuleServices.h>
#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TuyaSmartCameraKit/TuyaSmartCameraKit.h>
#import <TuyaSmartDeviceKit/TuyaSmartHomeManager.h>
#import <TuyaSmartDeviceKit/TuyaSmartHome.h>
//#import <TYModuleServices/TYActivatorProtocol.h>
#import "TuyaHome.h"

@interface TuyaViewController () <TuyaSmartHomeManagerDelegate>
@property (strong,nonatomic) UIButton *backBtn;
@property (strong,nonatomic) UIButton *addBtn;
@property (strong,nonatomic) UITextView *titleView;
@property (strong, nonatomic) TuyaSmartHomeManager *homeManager;
@end

@implementation TuyaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.addBtn];
    [self.view addSubview:self.titleView];
    [[TuyaSmartBizCore sharedInstance] updateConfig];
    [self initiateCurrentHome];
    [[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYSmartHomeDataProtocol) withInstance:self];
    [[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYSmartHouseIndexProtocol) withInstance:self];
    [[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYFamilyProtocol) withInstance:self];
}


- (void)viewDidAppear:(BOOL)animated {
    if ([TuyaHome getCurrentHome]) {
        
    }
}
- (void)initiateCurrentHome {
    self.homeManager.delegate = self;
    [self.homeManager getHomeListWithSuccess:^(NSArray<TuyaSmartHomeModel *> *homes) {
        if (homes && homes.count > 0) {
            [TuyaHome setCurrentHome:homes.firstObject];
        }
        NSLog(@"111");
    } failure:^(NSError *error) {
        NSLog(@"111");
    }];
}

- (TuyaSmartHome *)getCurrentHome {
    if (![TuyaHome getCurrentHome]) {
        return  nil;
    }
    
    TuyaSmartHome *home = [TuyaSmartHome homeWithHomeId:[TuyaHome getCurrentHome].homeId];
    return home;
}

-(void)homeManager:(TuyaSmartHomeManager *)manager didAddHome:(TuyaSmartHomeModel *)home{
    
}
-(void)closePage{
    [self dismissViewControllerAnimated:NO completion:nil];
}
-(void)addDevice{
    id<TYActivatorProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYActivatorProtocol)];
    [impl gotoCategoryViewController];
    // 获取配网结果
    [impl activatorCompletion:TYActivatorCompletionNodeNormal customJump:NO completionBlock:^(NSArray * _Nullable deviceList) {
        NSLog(@"deviceList: %@",deviceList);
    }];
}

-(UIButton *)backBtn{
    if (_backBtn == nil) {
        _backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 44, 50, 30)];
    }
    [_backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_backBtn setTitle:@"返回" forState:UIControlStateNormal];
    _backBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_backBtn addTarget:self action:@selector(closePage) forControlEvents:UIControlEventTouchUpInside];
    return _backBtn;
}

-(UIButton *)addBtn{
    if (_addBtn == nil) {
        _addBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60, 44, 50, 30)];
    }
    [_addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_addBtn setTitle:@"添加" forState:UIControlStateNormal];
    _addBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_addBtn addTarget:self action:@selector(addDevice) forControlEvents:UIControlEventTouchUpInside];
    return _addBtn;
}

-(UITextView *)titleView{
    if (_titleView == nil) {
        _titleView = [[UITextView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 44, 100, 30)];
    }
    [_titleView setText:@"监控摄像"];
    [_titleView setFont:[UIFont systemFontOfSize:15]];
    return _titleView;
}
- (TuyaSmartHomeManager *)homeManager {
    if (!_homeManager) {
        _homeManager = [[TuyaSmartHomeManager alloc] init];
    }
    return _homeManager;
}
@end
